package com.lijing.controller;

import com.lijing.model.News;
import com.lijing.model.User;
import com.lijing.service.ClubService;
import com.lijing.service.NewsService;
import com.lijing.util.JSONResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class NewsController {
    @Autowired
    private NewsService newsService;
    @Autowired
    private ClubService clubService;

    @RequestMapping("/getNewsListByClub")
    public List<News> getNewsListByClub(Integer cid){
        return newsService.getNewsListByClub(cid);
    }

    @RequestMapping("/getNewsList")
    public List<News> getNewsList(){
        return newsService.getNewsList();
    }

    @RequestMapping("/addNews")
    public String addNews(News news, HttpSession session){
        User user = (User) session.getAttribute("user");
        news.setAuthor(user);
        news.setClub(user.getClub());
        newsService.addNews(news);
        return JSONResponse.getOK("添加成功");
    }

    @RequestMapping(value = "/deleteNews",produces = "application/json;charset=UTF-8")
    public String deleteNews(Integer[] nids){
        for (Integer nid:nids){
            newsService.deleteNews(nid);
        }
        return JSONResponse.getOK("删除成功！");
    }
}
